// import Vue from 'vue';
import store from '@/store'
import axios from 'axios'
import {
  Notification,
  MessageBox,
  Message,
  Loading
} from 'element-ui'
import errorCode from '@/utils/errorCode'


axios.defaults.timeout = 30000;
axios.defaults.baseURL = 'http://192.168.0.103:8080';

//http request 拦截器
axios.interceptors.request.use(
  config => {
    const token = sessionStorage.getItem('token');

    // config.data = JSON.stringify(config.data);
    // config.headers = {
    //   'Content-Type': 'application/x-www-form-urlencoded'
    // }

    if (token) {
      config.params.token = token;
    }
    // let lang = localStorage.getItem('lang');
    // console.log(store)
    let lang = store.state.lang;
    // console.log(config.method != 'post')
    if (config.method != 'post') {
      if (lang) {
        config.params.lang = (lang == 'EN' ? 'EN' : 'CN');
      } else {
        config.params.lang = 'CN';
      }
    }

    // console.log('config.params', config.params);
    return config;
  },
  error => {
    return Promise.reject(err);
  }
);

//在main.js设置全局的请求次数，请求的间隙
axios.defaults.retry = 1;
axios.defaults.retryDelay = 1000;

// 响应拦截器
axios.interceptors.response.use(res => {
    // 未设置状态码则默认成功状态
    const code = res.data.code || 200;
    // 获取错误信息
    const msg = errorCode[code] || res.data.msg || errorCode['default']
    // 二进制数据则直接返回
    if (code !== 200) {
      Notification.error({
        title: msg
      })
      return Promise.reject('error')
    } else {
      // return res.data
      return res
    }
  },
  error => {
    console.log('err' + error)
    let {
      message
    } = error;
    if (message == "Network Error") {
      message = "后端接口连接异常";
    } else if (message.includes("timeout")) {
      message = "系统接口请求超时";
    } else if (message.includes("Request failed with status code")) {
      message = "系统接口" + message.substr(message.length - 3) + "异常";
    }
    Message({
      message: message,
      type: 'error',
      duration: 5 * 1000
    })
    return Promise.reject(error)
  }
)


//http response 拦截器
// axios.interceptors.response.use(
//     response => {
//         if(response.data.errCode ==2){
//             router.push({
//                 path:"/login",
//                 querry:{redirect:router.currentRoute.fullPath}//从哪个页面跳转
//             })
//         }
//         return response;
//     },
//     error => {
//         return Promise.reject(error)
//     }
// )


/**
 * 封装get方法
 * @param url
 * @param data
 * @returns {Promise}
 */

export function get(url, params = {}) {
  return new Promise((resolve, reject) => {
    axios.get(url, {
        params: params
      })
      .then(response => {
        resolve(response.data);
      })
      .catch(err => {
        reject(err)
      })
  })
}




/**
 * 封装post请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function post(url, data = {}) {
  return new Promise((resolve, reject) => {
    axios.post(url, data)
      .then(response => {
        resolve(response.data);
      }, err => {
        reject(err)
      })
  })
}
